package dryven.codegen.asm.typeinfo;

import org.objectweb.asm.Type;


public class ClassTypeInfo extends AbstractTypeInfo {

	private Class<?> _type;
	
	public ClassTypeInfo(Class<?> type) {
		super();
		_type = type;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ClassTypeInfo other = (ClassTypeInfo) obj;
		if (_type == null) {
			if (other._type != null)
				return false;
		} else if (!_type.equals(other._type))
			return false;
		return true;
	}

	@Override
	public String getTypeDescriptor() {
		return Type.getDescriptor(_type);
	}

	@Override
	public String getTypeName() {
		return _type.getName();
	}

	@Override
	public boolean isBoolean() {
		return _type.equals(Boolean.TYPE);
	}

	@Override
	public boolean isByte() {
		return _type.equals(Byte.TYPE);
	}

	@Override
	public boolean isChar() {
		return _type.equals(Character.TYPE);
	}

	@Override
	public boolean isDouble() {
		return _type.equals(Double.TYPE);
	}

	@Override
	public boolean isFloat() {
		return _type.equals(Float.TYPE);
	}

	@Override
	public boolean isInt() {
		return _type.equals(Integer.TYPE);
	}

	@Override
	public boolean isLong() {
		return _type.equals(Long.TYPE);
	}

	@Override
	public boolean isReference() {
		return !_type.isPrimitive();
	}

	@Override
	public boolean isShort() {
		return _type.equals(Short.TYPE);
	}

	@Override
	public String getInternalClassName() {
		if(isReference()) {
			return getTypeName().replace('.', '/');
		} else {
			throw new RuntimeException("not an object");
		}
	}

	@Override
	public boolean isVoid() {
		return _type.equals(Void.TYPE);
	}

	@Override
	public boolean isInterface() {
		return _type.isInterface();
	}

	@Override
	public boolean isArray() {
		return _type.isArray();
	}

	@Override
	public TypeInfo getSuperType() {
		if(_type.getSuperclass()==null) {
			return null;
		}
		return new ClassTypeInfo(_type.getSuperclass());
	}

	@Override
	public boolean isAssignableTo(Class<?> clazz) {
		return clazz.isAssignableFrom(_type);
	}
}
